package com.guodi.common.utils;

import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SoeUtil {

	/**
	 * 通过url获取列表
	 * @入参： url
	 * @入参： param
	 * @return
	 * @throws Exception
	 */
	public static String callRestSoeURLService(String url,Map<String,String> param) throws Exception{
		if(param == null) {
			param = new HashMap<>();
		}
		if(!param.containsKey("f")){
			param.put("f", "json");
		}
		HttpRequester request = new HttpRequester();
		HttpRespons respons = request.sendPost2(url, param);
		String json = respons.getContent();
		return json;
	}

	/**
	 * 通过url获取列表 陈献斌
	 * @入参： url
	 * @入参： param
	 * @return
	 * @throws Exception
	 */
	public static byte[] callRestSoeURLServiceInputStream(String url, Map<String,String> param) throws Exception{
		if(param == null) {
			param = new HashMap<String,String>();
		}
		if(!param.containsKey("f")){
			param.put("f", "json");
		}
		HttpRequester request = new HttpRequester();
		HttpRespons respons = request.sendPost2(url, param);
		byte[] json = respons.getBytay();
		return json;
	}

	/**
	 * 递归设置树节点是否选中
	 * @入参： treeList	树数据集
	 * @入参： codes		选中节点数组
	 * @throws Exception
	 */
	public static void recursiveData(List<PageData> treeList, String[] codes) throws Exception{
		if(codes!=null && codes.length>0){
			if(treeList != null && treeList.size() > 0){
				for(PageData treePd : treeList){
					for(String code : codes){
						if(treePd.getString("land_code").equals(code)){
							treePd.put("checked", true);
							break;
						}
					}
					recursiveData((List<PageData>)treePd.get("children"),codes);
				}
			}
		}
	}

	/**
	 * 调用发布好的SOE服务
	 * @入参： url 配置在yaml的soe地址
	 * @入参： param soe服务调用所需的参数
	 * @出参： 1、处理得到结果，返回未经处理过的json数据
	 * 2、处理过程中出错，返回{success:false,message:XXXXXXX}
	 * @throws IOException
	 */
	public static String callRestSoeService(String url, Map<String,String> param) throws Exception{
		if(StringUtils.isBlank(url)){
			throw new Exception("soe服务图层地址为空。");
		}
		if(param == null) {
			param = new HashMap<String,String>();
		}
		if(!param.containsKey("f")){
			param.put("f", "json");
		}
		HttpRequester request = new HttpRequester();
		HttpRespons respons = request.sendPost2(url, param);
		String json = respons.getContent();
		return json;
	}
}
